﻿Imports System.Data.SqlClient
Public Class Questions
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Assign text to the heading
        If Request.Browser.IsMobileDevice Then
            Response.Redirect("~/Questions_mobile.aspx")
        End If
        myHeading.InnerHtml = "Questions And Answers"

        If IsPostBack = False Then

            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("BooksReviewDBConnectionString1").ConnectionString.ToString)
            Dim SqlString As String = "INSERT INTO Site_Activity(Activity) VALUES (@f1)"
            Dim cmd As SqlCommand = New SqlCommand(SqlString, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", "Q&A")
            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()
        End If
    End Sub

    Protected Sub SqlDataSource2_Selected(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs) Handles SqlDataSource2.Selected
        'If datasource is empty
        If (e.AffectedRows < 1) Then
            myHeading.InnerHtml = "No Books found"
        End If
        If (e.AffectedRows < 13) Then
            DataPagerBooks.Visible = False
        Else
            DataPagerBooks.Visible = True
        End If
    End Sub
    Protected Sub ListView1_DataBound(sender As Object, e As EventArgs)
        'Display datapager only if there is more than one page to display
        DataPagerBooks.Visible = (DataPagerBooks.PageSize < DataPagerBooks.TotalRowCount)
    End Sub

    Protected Sub btn_findByName_Click(sender As Object, e As EventArgs) Handles btn_findByName.Click
        searchDatabase("BookName", tb_search.Text)
    End Sub

    Private Function searchDatabase(ByVal buttonStr As String, ByVal searchStr As String)
        SqlDataSource2.SelectCommand = "SELECT * FROM Books_Questions WHERE " & buttonStr & " LIKE '%" & searchStr & "%' AND IsVerified = 1 ORDER BY Question_Added DESC"
        SqlDataSource2.DataBind()
        Return True
    End Function
End Class